<!--
# CIS 2515 FINAL PROJECT
# Author: Malacarne, Nicholas SID: 7770
# Description: This script handles all of the game play as well as generating
# the battle game page.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
include('../Lib/php/PlayerDef.php');
include('../Lib/php/GlyphDef.php');
include('../Lib/php/SuperHTMLDef.php');
include('../Lib/php/DBMgrDef.php');
?>
<?php session_start(); ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Glyph</title>
    </head>
    <body>
<?php

$HTML = new SuperHTMLDef("Glyph");
$HTML->buildTop();
$HTML->addHTML("<center>");
$HTML->addHTML($HTML->image("./logos/glyphlogo.png"));

if (!isset($_REQUEST["page"]))
{
    $HTML->addHTML(buildLoginBox());
}
else if ($_REQUEST["page"] == "login")
{
    if (userExists($_REQUEST["accountName"]) &&
        passwordCheck($_REQUEST["password"]))
    {
        $_SESSION["player1"] = $_REQUEST["accountName"];

        echo $_SESSION["player1"];
        echo $_SESSION["player2"];
        $HTML->tag("b", "Login Successful!");
    }
    else
    {
        $HTML->addHTML($HTML->h3("Login:"));
        $HTML->addHTML(buildLoginBox());
        $HTML->tag("b", "User/Password Invalid!");
    }
}

echo $HTML->getPage();

/***************************************************************************** *
* Function Name: passwordCheck
* Author: Nicholas Malacarne
* Parameters: $password - The user's entered password.
* Description: Checks to see if the password entered matches the password stored.
***************************************************************************** */
function passwordCheck($password)
{
    $DB = new DBMgrDef('localhost', 'root', 'praisera87', 'glyph');

    $account = $DB->fetchRow("accounts", $_SESSION["id"], "id");

    if ($account["password"] == $password)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/***************************************************************************** *
* Function Name: userExists
* Author: Nicholas Malacarne
* Parameters: $accountName - The users' account name.
* Description: Checks to see if the account exists within the DB.
***************************************************************************** */
function userExists($accountName)
{
    $DB = new DBMgrDef('localhost', 'root', 'praisera87', 'glyph');

    for ($i = 1; $i <= $DB->getRows("accounts"); $i++)
    {
        $account = $DB->fetchRow("accounts", $i, "id");

        if ($account["userName"] == $accountName)
        {
            $_SESSION["id"] = $i;
            $bFound = true;
            break;
        }
    }

    unset($DB);

    if ($bFound)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/***************************************************************************** *
* Function Name: buildLoginBox
* Author: Nicholas Malacarne
* Parameters:
* Description: Builds the table to hold the login data.
***************************************************************************** */
function buildLoginBox()
{
    $HTML = new SuperHTMLDef();

 /* Starts the table.
  */$HTML->startTable();
    $HTML->addHTML("<form method=\"post\" action=\"login.php\">");
    $HTML->addHTML("<tr>");
    $HTML->tData($HTML->gTag("b", "Account Name:"));
    $HTML->tData($HTML->gTextbox("text", "accountName"));
    $HTML->addHTML("</tr>");
    $HTML->addHTML("<tr>");
    $HTML->tData($HTML->gTag("b", "Password:"));
    $HTML->tData($HTML->gTextbox("password", "password"));
    $HTML->addHTML("</tr>");
    $HTML->addHTML("<tr>");
    $HTML->tData($HTML->gSubmit("Login"), "center", "2");
    $HTML->textbox("hidden",
                   "page",
                   "login");
    $HTML->addHTML("</tr>");
    $HTML->addHTML("</form>");
    $HTML->endTable();

    return $HTML->getPage();

}

?>
    </body>
</html>
